import web
import json
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker, scoped_session



engine = create_engine('mysql://root:th@127.0.0.1/test')
db = scoped_session(sessionmaker(bind=engine))

Base = declarative_base()
class User(Base):
  __tablename__ = 'user'
  id = Column(Integer, primary_key = True)
  username = Column(String(40))
  password = Column(String(40))
  
  def __repr__(self):
    return "<User(id='%s', username='%s', password='%s')>" % (self.id, self.username, self.password)
    
    
urls = (
    '/', 'index'
)

class index:
    def GET(self):
        json_results = []
        for result in db.query(User).all(): 
          d = {'id': result.id,
               'username': result.username,
               'password': result.password}
          json_results.append(d)
        return json.dumps(json_results)

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()
